<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>URL编码</title>

<script type="text/javascript">

// 编码表
var indexTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

// 将参数转化为Unicode编码
function ParamToUCS(param) {
	var rgExpPlus = /\+/g;
	param = escape(param).replace(rgExpPlus, "%2B");
	
	// 将ASCII中非数字字母符号转化为Unicode
	var rgExpToken = /(%([0-9A-Fa-f]{2}))/g;
	var resultArray = param.match(rgExpToken);
	if (resultArray) {
		for (var ii = 0; ii < resultArray.length; ++ii) {
			var subStr = resultArray[ii].substring(1, 3);
			param = param.replace(resultArray[ii], '%u00' + subStr);
		}
	}
	
	return param;
}

// 扩展Base64编码
function ExtBase64Encode(param) {
	// 获得Unicode编码
	var unicode = ParamToUCS(param);
	
	var encodeStr = "";	
	for (var i = 0; i < unicode.length; ++i) {
		if (unicode.charAt(i) == "%" && unicode.charAt(i + 1) == "u") {
			var subStr = unicode.substring(i + 2, i + 6);
			var binaryStr = parseInt(subStr, 16) << 2;
			
			// 0x3E0 0011 1110 0000
			if ((binaryStr & 0x00F80) == 0x00F80) {
				// 0x0FDFF 0000 1111 1101 1111 1111
				binaryStr = (binaryStr & 0xFFDFF) | 0x00001;
				// alert("change");
			}
			//0x3F 0011 1111
			var num_1 = binaryStr & 0x0003F;
			//0xFC0 1111 1100 0000
			var num_2 = (binaryStr & 0x00FC0) >> 6;
			// 0x3F000 0011 1111 0000 0000 0000
			var num_3 = (binaryStr & 0x3F000) >> 12;
			
			encodeStr += String(indexTable.charAt(num_3)) + String(indexTable.charAt(num_2)) 
				+ String(indexTable.charAt(num_1));
			i += 5;
		} else {
			encodeStr += "$" + unicode.substring(i, i + 1);	
		}
	}
	
	return encodeStr;
}


var boolean = 1;

var paramId = 2;
function getParamId() {
	return paramId++;
}
// 添加另一个参数
function addParameter() {
	if (boolean == 0) return;
	
	var parentDiv=document.getElementById('pid');
	var input = document.createElement("div");
	input.innerHTML = "参数" + getParamId() + ": <input type='text' id=p" + (paramId - 1) + " value='请输入参数' />";
	parentDiv.appendChild(input); 
}

function createParam(type) {
	var param = "";
	for (var i = 1; i < paramId; ++i) {
		if (i != 1) {
			param += "&";
		}
		paraText = document.getElementById("p" + String(i));
		if (type == 1) param += "name" + String(i) + "=" + ExtBase64Encode(paraText.value);
		else if (type == 2) param += "name" + String(i) + "=" + paraText.value;
	}
	return param;
}

// 在新窗口打开链接
function toServer(type) {
	//createParam(type);
	 window.open('server' + String(type) + '.php?' + createParam(type));
}

</script>

</head>

<body>
    <!--<a href="#" onclick="javascript: window.location.href='server.php?' + createParam()">百度一下</a>-->
    <form id="myForm" method="get">
    	<table>
	    	<tr>
            	<td><input type="button" value="继续添加" onclick="addParameter()"/></td>
                <td><input type="button" id="SUBMIT" value="压缩提交" onclick="toServer(1)" /></td>
                <td><input type="button" id="SUBMIT" value="直接提交" onclick="toServer(2)" /></td>
            </tr>
        </table>
   		 参数1: <input type="text" id="p1" value="请输入参数"/>
         <td><div id= "pid"> </div></td> 
    </form>
</body>
</html>